package com.nyp.remoter.web;

import java.io.Serializable;
import java.util.concurrent.Future;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.event.AjaxBehaviorEvent;
import javax.inject.Named;

import com.nyp.remoter.ejb.HelloServiceLocal;

@Named
@SessionScoped
public class ProbaBean implements Serializable{
	
	@EJB
	HelloServiceLocal helloService;
	
	@PostConstruct
	public void init(){
		System.out.println("ProbaBean initialized");
	}
	
	private String greeting = "Hello";
	
	private String processStatus = "Process not yet started";
	
	public String getGreeting() {
		return greeting;
	}

	public void setGreeting(String greeting) {
		this.greeting = greeting;
	}
	
	private Future<String> processStatusFut;
	public String startLongProcess(AjaxBehaviorEvent evt){
		System.out.println("start long process started");
		processStatusFut = helloService.doLongProcess();
		System.out.println("ProbaBean helloService.doLongProcess() ended. processStatusFut="+ processStatusFut);
		processStatus = "Process started";
		System.out.println(processStatus);
		return null;
	}
	
	public String refreshProcessStatus(AjaxBehaviorEvent evt){
		System.out.println("refreshProcessStatus started");
		if(processStatusFut == null){
			processStatus = "Process not yet started";
		}
		else{
			if(processStatusFut.isDone()){
				processStatus = "Process completed!!";
			}
			else{
				processStatus = "Process is working...";
			}
		}
		System.out.println(processStatus);
		return null;
	}
	
	public String getProcessStatus(){
		return processStatus;
	}

	public void setProcessStatus(String processStatus) {
		this.processStatus = processStatus;
	}
	

}
